<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27/0027
 * Time: 16:30
 */

namespace app\sports_admin\controller;
use app\exception\AdminException;
use app\exception\AdminRuleException;
use app\sports_admin\model\Rule;
use think\Db;
use think\Exception;
use think\Request;
use app\sports_admin\service\Admin as AdminService;
use app\sports_admin\model\Admin as AdminModel;
use app\sports_admin\model\AdminRule as AdminRuleModel;
class Admin extends Base
{

    protected $beforeActionList = [
        'isAdmin'  =>  ['only'=>'insert,delete,updaterule'],
        'operationDelete'   =>  ['only'=>'delete'],
        'operationSelect'   =>  ['only'=>'add,getadminlist'],
        'operationInsert'   =>  ['only'=>'insert'],
        'operationUpdate'   =>  ['only'=>'updaterule,updatepass'],
    ];
    /**
     * 添加管理员页面
     */
    public function add()
    {
        $rule = Rule::where('scope','<>',128)->select();
        return view('add',compact('rule'));
    }
    /**
     * 执行管理员添加
    */
    public function insert(Request $request)
    {
        if($request->isAjax()){
            $result = AdminService::adminInsert();
            if($result){
                return json(['code'=>201,'msg'=>'添加成功']);
            }else{
                throw new Exception('服务器内部错误');
            }
        }
    }
    /**
     * 管理员列表页
     */
    public function index()
    {
        return view('index');
    }

    /**
     * 获取管理员列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function getAdminList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = AdminModel::getAdminInfo($page,$rows,$sort,$sortOrder,$searchName);
        return json($result);
    }

    /**
     * 编辑管理员权限
     * @param $adminId 管理员id
     */
    public function editRule(Request $request)
    {
        $adminId = $request->get('adminId');
        $info = AdminModel::getAdminRule($adminId);
        $rule =  $rule = Rule::where('scope','<>',128)->select();
        return view('editRule',compact('info','rule'));
    }
    /**
     * 修改管理员权限
     * @param $adminId 管理员id
     * @param $adminRuleId 管理员权限中间表id
     * @param $ruleId  权限级别id
    */
    public function updateRule()
    {
        $params = Request::instance()->only(['adminId','ruleId']);
        $bool = AdminRuleModel::updateRule($params['adminId'],$params['ruleId']);
        if(!$bool){
            throw new AdminRuleException([
                'msg'=>'权限修改失败',
                'code'=>400
            ]);
        }
        return json(['code'=>201,'msg'=>'权限修改成功']);
    }

    /**
     * 删除管理员操作
     * @param $adminId 管理员id
     */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            $adminId = $request->post('adminId');
            Db::startTrans();
            $bool = AdminModel::get($adminId)->together('adminRule')->delete();
            if(!$bool){
                Db::rollback();
                throw new AdminException([
                    'msg'=>'管理员删除失败',
                    'errorCode'=>203
                ]);
            }
            Db::commit();
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    /**
     * 修改密码页面
    */
    public function pass()
    {
        return view('pass');
    }
    /**
     * 修改密码操作
     * @param $pass新密码
     * @param $password 新密码
    */
    public function updatePass(Request $request)
    {
        if($request->isAjax()){
            return AdminService::updatePass();
        }
    }
}